package com.hzdl.dao.impl;

import com.hzdl.dao.AccountDao;
import com.hzdl.domain.Account;
import com.hzdl.util.ConnectionUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author ：Reus CAI
 * @date ：Created in 2020/12/15 10:23
 * @description：
 * @modified By：
 * @version: $
 */

public class AccountDaoImpl implements AccountDao {

    private QueryRunner queryRunner;
    private ConnectionUtil connectionUtil;

    public void setConnectionUtil(ConnectionUtil connectionUtil) {
        this.connectionUtil = connectionUtil;
    }

    public void setQueryRunner(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }

    @Override
    public Account findByID(Integer id) {

        Account account = null;
        String sql = "select * from account where id = ?";

        try {
            account = queryRunner.query(this.connectionUtil.getConnection(), sql, new BeanHandler<Account>(Account.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return account;
    }

    @Override
    public void update(Integer id, Double money) {
        String sql = null;
        sql = "update account set money=money+? where id =?";

        try {
            int update = queryRunner.update(this.connectionUtil.getConnection(),sql,money,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }


}
